package com.pxmeta.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.party.entity.dto.AddDecorationDTO;
import com.pxmeta.product.entity.Shop;
import com.pxmeta.product.service.IShopService;
import com.pxmeta.product.service.IUserShopService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "商城管理",tags = "商城管理")
@CrossOrigin
@RequestMapping(value = "/shop")
@RequiredArgsConstructor
public class ShopController {

    private final IShopService shopService;

    private final IUserService userService;
    private final IUserShopService userShopService;

    @GetMapping("/getShopList")
    @ApiOperation("获取礼物列表")
    public AjaxResult<List<Shop>> getShopList(@RequestParam(required = false) Integer category,
                                              @RequestParam Integer page,
                                              @RequestParam Integer size){
        IPage<Shop> result = shopService.getDecorationList(page, size, category);
        return AjaxResult.successPage(result.getRecords(), result.getTotal());
    }

    @PutMapping("/updateDecoration")
    @ApiOperation("修改装扮")
    public AjaxResult updateDecoration(@RequestBody Shop shop) {

        shopService.updateById(shop);

        return AjaxResult.success();
    }

    @GetMapping("/giveShop")
    @ApiOperation("赠送装扮")
    public AjaxResult giveShop(@RequestParam String account,
                               @RequestParam Long shopId,
                               @RequestParam Integer days){
        //将虚拟礼物放入背包。
        User user = userService.getByAccount(account);
        Shop shop = shopService.getById(shopId);
        userShopService.add(user.getId(), shop, days);
        return AjaxResult.success();
    }
    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("根据商品id删除")
    public AjaxResult deleteById(@PathVariable Long id){
        shopService.removeById(id);
        return AjaxResult.success();
    }


    @PostMapping("/addDecoration")
    @ApiOperation("添加装扮")
    public AjaxResult addDecoration(@RequestBody AddDecorationDTO dto){
        shopService.addDecoration(dto);
        return AjaxResult.success();
    }


}
